import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:core_package/core_package.dart';
import 'package:flutter_app_unionad/core/configuration.dart';
import 'package:flutter_app_unionad/ui/basice/base_center_alert_widget.dart';
import 'package:flutter_app_unionad/ui/basice/input_separator_widget.dart';
import 'package:flutter_app_unionad/ui/basice/my_button.dart';
import 'package:flutter_app_unionad/ui/my_facility/widget/select_pay_type_widget.dart';

//新购设备弹框
// ignore: must_be_immutable

class AddFacilityWidget extends StatefulWidget {
  final Function(String number)? reloadOnTap;
  final int? max;
  final bool isMoney; //是否是收益购买

  const AddFacilityWidget({Key? key, this.reloadOnTap, this.max, this.isMoney = false}) : super(key: key);

  @override
  _AddFacilityWidgetState createState() => _AddFacilityWidgetState();
}

class _AddFacilityWidgetState extends State<AddFacilityWidget> with BaseCenterAlertWidget {
  String? number;

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return buildCloseAlertWidget(
      context,
      child: GestureDetector(
        behavior: HitTestBehavior.opaque,
        onTap: () {
          FocusScope.of(context).requestFocus(FocusNode());
        },
        child: Container(
          padding: EdgeInsets.only(
            top: 30.px,
            left: 40.px,
            right: 30.px,
            bottom: 30.px,
          ),
          child: Column(
            children: [
              Text(
                "购买设备",
                style: TextStyle(
                  color: MyColors.formHex("#272727FF"),
                  fontSize: MyFont.font16,
                  fontWeight: MyFontWeight.bold,
                ),
              ),
              MySizeBox(height: 20),
              InputSeparatorWidget(
                valueChange: (value) {
                  //购买
                  setState(() {
                    number = value;
                  });
                },
                title: "购买",
                placeholder: "最多${widget.max}台",
                keyboard: TextInputType.number,
                titleWidth: 50.px,
                icon: Text(
                  "${Configuration().price}元/台",
                  style: TextStyle(
                    color: MyColors.color_27_27_27,
                    fontSize: MyFont.font13,
                    fontWeight: MyFontWeight.regular,
                  ),
                ),
              ),
              MySizeBox(height: 40),
              Center(
                child: Row(
                  mainAxisSize: MainAxisSize.min,
                  children: [
                    Text(
                      "需要购买更多设备，",
                      style: TextStyle(
                        color: MyColors.formHex("#A0A0A0FF"),
                        fontSize: MyFont.font13,
                        fontWeight: MyFontWeight.regular,
                      ),
                    ),
                    GestureDetector(
                      onTap: () {
                        //
                        callService(context);
                      },
                      child: Text(
                        "点击联系客服专员",
                        style: TextStyle(
                          decoration: TextDecoration.underline,
                          color: MyColors.formHex("#4CC2FFFF"),
                          fontSize: MyFont.font13,
                          fontWeight: MyFontWeight.regular,
                        ),
                      ),
                    )
                  ],
                ),
              ),
              MySizeBox(height: 20),
              Center(
                child: MyButton(
                  title:
                      "支付（${(number == null || number!.stringIsEmpty) ? "0" : int.parse(number ?? '0') * int.parse(Configuration().price)}）",
                  onTap: () {
                    payOnTap(context);
                  },
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }

  // @override
  // Widget build(BuildContext context) {
  //   return ;
  // }

  void payOnTap(BuildContext context) {
    if (number == null || number!.length == 0) {
      ShowToastWidget.showToast(msg: "请输入购买数量");

      return;
    }
    FocusScope.of(context).requestFocus(FocusNode());

    if (int.parse(number ?? '0') > widget.max!) {
      ShowToastWidget.showToast(msg: "购买数量不能超过${widget.max}");

      return;
    }

    //快速支付
    Navigator.pop(context);
    if (widget.reloadOnTap != null) {
      widget.reloadOnTap?.call(number ?? '0');
    }
  }

  void callService(BuildContext context) {
    FocusScope.of(context).requestFocus(FocusNode());
    Configuration().call();

    //拨打电话
  }
}
